package org.jetnuke.servlets.utilities;

import java.io.File;
import java.util.Calendar;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletContext;

public class Util {
	static Hashtable resourcesTable=new Hashtable();
	ServletContext servletContext;
	public Util(ServletContext servletContext) {
		this.servletContext=servletContext;
		
	}
	public int getMonth(Calendar gc){
		return gc.get(Calendar.MONTH);
	}
	public int getYear(Calendar gc){
		return gc.get(Calendar.YEAR);
	}
	public int getDay(Calendar gc){
		return gc.get(Calendar.DAY_OF_MONTH);
	}
	public String getDayS(Calendar gc){
		return String.valueOf(gc.get(Calendar.DAY_OF_MONTH));
	}
	public boolean fileExists(String resourceFile){
		//System.out.println(resourceFile);
		try {
			String path=servletContext.getRealPath("WEB-INF/classes/"+resourceFile);
			Boolean exists=(Boolean)resourcesTable.get(path);
			if(exists==null){
				File f = new File(path);
				if(f.exists()&&f.canRead()){
					exists=Boolean.TRUE;
				}else{
					exists=Boolean.FALSE;
				}
				resourcesTable.put(path,exists);
			}
			return exists.booleanValue();
		} catch (Throwable e) {
		}
		return false;
	}
	public boolean existsOnSortedList(Object element, List sortedList){
		int index = Collections.binarySearch(sortedList, element);   
		return index>-1;
	}
}
